/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package naivebayes;

/**
 *
 * @author jaime
 */
public class ConfusionMatrix {
    
    private int truePositives;
    private int falsePositives;
    private int trueNegatives;
    private int falseNegatives;
    
    public ConfusionMatrix()
    {
        truePositives = 0;
        falsePositives = 0;
        trueNegatives = 0;
        falseNegatives = 0;
    }
    
    
    public void newOccurrence(boolean isPositive, boolean classifiedPositive)
    {
        if(isPositive && classifiedPositive)
        {
            truePositives++;
        }
        if(isPositive && !classifiedPositive)
        {
            falseNegatives++;
        }
        if(!isPositive && !classifiedPositive)
        {
            trueNegatives++;
        }
        if(!isPositive && classifiedPositive)
        {
            falsePositives++;
        }
    }

    public int getFalseNegatives() {
        return falseNegatives;
    }

    public int getFalsePositives() {
        return falsePositives;
    }

    public int getTrueNegatives() {
        return trueNegatives;
    }

    public int getTruePositives() {
        return truePositives;
    }
    
    public double getPrecision()
    {
        return truePositives*1.0/(truePositives+falsePositives);
    }
    
    public double getRecall()
    {
        return truePositives*1.0/(truePositives+falseNegatives);
    }
    
    public double getFalsePositivesRate()
    {
        return falsePositives*1.0/(falsePositives+trueNegatives);
    }
    
    //public static LearningAnalyzer 
    
    public double getFMeasure()
    {
        double precision = getPrecision();
        double recall = getRecall();
        
        return (2*precision*recall)/(precision+recall);
        
    }
    
    public String toString()
    {
        return confusionMatrixToString()+"\nPrecision = "+getPrecision()+
                "\nTVP (recall) = "+getRecall()+"\nTFP = "+getFalsePositivesRate()+
                "\nF-Measure = "+getFMeasure();
    }

    public String confusionMatrixToString() {
        String matrix = "                predicted class\n               | P | N\n";
        matrix += "real class | P | "+truePositives +" | "+falseNegatives+"\n"+"           | N | "+falsePositives+" | "+trueNegatives+"\n";
        return matrix;
    }
    
}
